﻿using System;
using System.Windows.Forms;
using System.Xml;
using RibbonEditor.AppCode;

namespace RibbonEditor
{
    public class CommandDefinition: IXmlBuildable
    {
        XmlNode commandDefinitionXmlNode;

        public string Id { get; set; }
        public EnableRules EnableRules { get; set; }
        public DisplayRules DisplayRules { get; set; }
        public ActionsList Actions { get; set; }

        public CommandDefinition()
        {
            this.EnableRules = new EnableRules(true);
            this.DisplayRules = new DisplayRules(true);
            this.Actions = new ActionsList();
        }

        public CommandDefinition(XmlNode node) : this()
        {
            try
            {
                this.commandDefinitionXmlNode = node;

                this.Id = (string)XmlHelper.ExtractXmlAttributeToProperty2(node, "Id", XmlHelper.PropertyType.stringType);

                if (node.SelectSingleNode("EnableRules") != null)
                {
                    this.EnableRules = new EnableRules(node.SelectSingleNode("EnableRules"), true);
                }

                if (node.SelectSingleNode("DisplayRules") != null)
                {
                    this.DisplayRules = new DisplayRules(node.SelectSingleNode("DisplayRules"), true);
                }

                if (node.SelectSingleNode("Actions") != null)
                {
                    this.Actions = new ActionsList(node.SelectSingleNode("Actions"));
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandDefinitionsNode)
        {
            try
            {
                XmlNode commandDefinitionNode = XmlHelper.AddNode(commandDefinitionsNode, "CommandDefinition");
                XmlHelper.AddAttribute(commandDefinitionNode, "Id", this.Id);

                this.EnableRules.AddNode(commandDefinitionNode);
                this.DisplayRules.AddNode(commandDefinitionNode);
                this.Actions.AddActionsNode(commandDefinitionNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("CommandDefinition");
                node.ContextMenuStrip = new ContextMenuStrip();
                node.Tag = this;

                ContextMenuStripHelper.AddDisplayXmlMenu(node.ContextMenuStrip, false);

                this.EnableRules.BuildTreeViewElement(node);
                this.DisplayRules.BuildTreeViewElement(node);
                this.Actions.BuildTreeViewElement(node);

                node.Expand();

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
